// Functions associated with the SidebarAssets class
//-----------------

//-----------------
// Includes libraries and other files
#include "SidebarAssets.h"
//-----------------

//-----------------
//// CONSTRUCTOR - SidebarAssets
SidebarAssets::SidebarAssets( const Window& window, UserInterface* p_ui, const int& height, const int& side)
	: Sidebar( window, p_ui, height, side)
{}

//// DESTRUCTOR - ~SidebarAssets
SidebarAssets::~SidebarAssets() {}
//-----------------

//-----------------
//// FUNCTION - get_active_asset()
// return an instance of the currently cursored over asset
ModelAsset SidebarAssets::get_active_asset() {
	// Pre-conditions :- a valid asset number must currently be cursored over
	int a( get_asset());
	assert( a >= 0 && a <= assets_.size());

	for( std::list<ModelAsset>::iterator i( assets_.begin()); i != assets_.end(); ++i, --a)
		// when a reaches 0 then it's moved past enough nodes to reach the correct asset
		if( a == 0)
			return *i;

	MessageBox( NULL, "Error Code: 007", "SidebarAssets::get_active_asset", MB_OK);
	ModelAsset temp;
	return temp;
}

//// FUNCTION - mouseover_asset
// return true if an item in one of the lists in being cursored over
bool SidebarAssets::mouseover_asset( const int& x, const int& y) {
	// Pre-conditions :- none
	assert( true);

	int list_x( 0), list_y( 0), counter( 0);
	get_asset_list_coordinates( list_x, list_y);

	for( std::list<ModelAsset>::iterator i( assets_.begin()); i != assets_.end(); ++i, ++counter) {
		// if the button type is the same as the asset's type...
		if( i->get_asset_type() == get_active_button()) {
			// ... and the mouse is cursoring over the asset's location...
			if(( x >= list_x && x <= (list_x + (i->get_asset_name().length()*CHAR_WIDTH))) &&
				( y >= list_y  && y <= (list_y  + CHAR_HEIGHT)) ) {
					// ... then set the asset to being cursored over and return true.
					set_asset_mouseover( counter);
					return true;
			}

			// ... if it's not cursoring over it, then move the 'selection box' down (as the asset's don't have 'official' bounding boxes
			list_y += (CHAR_HEIGHT+5);
		}

	}

	// if none of the buttons are under the mouse then return false
	set_asset_mouseover( -1);
	return false;
}

//// FUNCTION - render
// output the sidebar
void SidebarAssets::render( LPDIRECT3DDEVICE9 device, const Window& window) {
	// Pre-conditions :- the device must exist
	assert( device != nullptr);

	// render the sidebar's background as well
	Sidebar::render( device, window);

	// render the lists of assets
	int x( 0), y( 0), limit( get_y() + get_height() - CHAR_HEIGHT - 4);
	get_asset_list_coordinates( x, y);

	for( std::list<ModelAsset>::iterator i( assets_.begin()); i != assets_.end(); ++i)
		if( i->get_asset_type() == get_active_button()) {
			if( y < limit)
				p_ui_->render_text( i->get_asset_name(), x, y, 255, 255, 255, 255);

			// move down in preparation for the next asset being written
			y += (CHAR_HEIGHT+5);
		}
}

//// FUNCTION - reset
// clear all the user-created data in preparation of a new module
void SidebarAssets::reset() {
	// Pre-conditions :- none
	assert( true);

	Sidebar::reset();
}